﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace RAG.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class RAGInit4 : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_conversations_app_users_user_id",
                table: "conversations");

            migrationBuilder.DropForeignKey(
                name: "FK_document_chunks_documents_document_chunk_document_id",
                table: "document_chunks");

            migrationBuilder.DropForeignKey(
                name: "FK_documents_app_users_user_id",
                table: "documents");

            migrationBuilder.DropForeignKey(
                name: "FK_knowledge_bases_app_users_user_id",
                table: "knowledge_bases");

            migrationBuilder.DropForeignKey(
                name: "FK_sessions_app_users_user_id",
                table: "sessions");

            migrationBuilder.DropForeignKey(
                name: "FK_user_roles_app_users_user_id",
                table: "user_roles");

            migrationBuilder.DropTable(
                name: "app_users");

            migrationBuilder.RenameColumn(
                name: "document_chunk_content",
                table: "document_chunks",
                newName: "content");

            migrationBuilder.RenameColumn(
                name: "document_chunk_token_count",
                table: "document_chunks",
                newName: "token_count");

            migrationBuilder.RenameColumn(
                name: "document_chunk_document_id",
                table: "document_chunks",
                newName: "document_id");

            migrationBuilder.RenameColumn(
                name: "document_chunk_chunk_index",
                table: "document_chunks",
                newName: "chunk_index");

            migrationBuilder.RenameColumn(
                name: "document_chunk_id",
                table: "document_chunks",
                newName: "id");

            migrationBuilder.RenameIndex(
                name: "IX_document_chunks_document_chunk_document_id",
                table: "document_chunks",
                newName: "IX_document_chunks_document_id");

            migrationBuilder.RenameIndex(
                name: "IX_document_chunks_document_chunk_chunk_index",
                table: "document_chunks",
                newName: "IX_document_chunks_chunk_index");

            migrationBuilder.CreateTable(
                name: "users",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false),
                    username = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
                    password = table.Column<string>(type: "character varying(255)", maxLength: 255, nullable: false),
                    salt = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
                    email = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
                    phone = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: true),
                    avatar = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
                    account_status = table.Column<int>(type: "integer", nullable: false, defaultValue: 0),
                    failed_login_attempts = table.Column<int>(type: "integer", nullable: false, defaultValue: 0),
                    locked_until = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    last_failed_login = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    last_activity_time = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    last_login_time = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    IsActive = table.Column<bool>(type: "boolean", nullable: false),
                    IsDeleted = table.Column<bool>(type: "boolean", nullable: false),
                    created_at = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    updated_at = table.Column<DateTime>(type: "timestamp with time zone", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_users", x => x.id);
                });

            migrationBuilder.CreateIndex(
                name: "IX_users_email",
                table: "users",
                column: "email",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_users_phone",
                table: "users",
                column: "phone");

            migrationBuilder.CreateIndex(
                name: "IX_users_username",
                table: "users",
                column: "username",
                unique: true);

            migrationBuilder.AddForeignKey(
                name: "FK_conversations_users_user_id",
                table: "conversations",
                column: "user_id",
                principalTable: "users",
                principalColumn: "id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_document_chunks_documents_document_id",
                table: "document_chunks",
                column: "document_id",
                principalTable: "documents",
                principalColumn: "document_id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_documents_users_user_id",
                table: "documents",
                column: "user_id",
                principalTable: "users",
                principalColumn: "id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_knowledge_bases_users_user_id",
                table: "knowledge_bases",
                column: "user_id",
                principalTable: "users",
                principalColumn: "id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_sessions_users_user_id",
                table: "sessions",
                column: "user_id",
                principalTable: "users",
                principalColumn: "id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_user_roles_users_user_id",
                table: "user_roles",
                column: "user_id",
                principalTable: "users",
                principalColumn: "id",
                onDelete: ReferentialAction.Cascade);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_conversations_users_user_id",
                table: "conversations");

            migrationBuilder.DropForeignKey(
                name: "FK_document_chunks_documents_document_id",
                table: "document_chunks");

            migrationBuilder.DropForeignKey(
                name: "FK_documents_users_user_id",
                table: "documents");

            migrationBuilder.DropForeignKey(
                name: "FK_knowledge_bases_users_user_id",
                table: "knowledge_bases");

            migrationBuilder.DropForeignKey(
                name: "FK_sessions_users_user_id",
                table: "sessions");

            migrationBuilder.DropForeignKey(
                name: "FK_user_roles_users_user_id",
                table: "user_roles");

            migrationBuilder.DropTable(
                name: "users");

            migrationBuilder.RenameColumn(
                name: "content",
                table: "document_chunks",
                newName: "document_chunk_content");

            migrationBuilder.RenameColumn(
                name: "token_count",
                table: "document_chunks",
                newName: "document_chunk_token_count");

            migrationBuilder.RenameColumn(
                name: "document_id",
                table: "document_chunks",
                newName: "document_chunk_document_id");

            migrationBuilder.RenameColumn(
                name: "chunk_index",
                table: "document_chunks",
                newName: "document_chunk_chunk_index");

            migrationBuilder.RenameColumn(
                name: "id",
                table: "document_chunks",
                newName: "document_chunk_id");

            migrationBuilder.RenameIndex(
                name: "IX_document_chunks_document_id",
                table: "document_chunks",
                newName: "IX_document_chunks_document_chunk_document_id");

            migrationBuilder.RenameIndex(
                name: "IX_document_chunks_chunk_index",
                table: "document_chunks",
                newName: "IX_document_chunks_document_chunk_chunk_index");

            migrationBuilder.CreateTable(
                name: "app_users",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false),
                    account_status = table.Column<int>(type: "integer", nullable: false, defaultValue: 0),
                    avatar = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
                    created_at = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    email = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
                    failed_login_attempts = table.Column<int>(type: "integer", nullable: false, defaultValue: 0),
                    IsActive = table.Column<bool>(type: "boolean", nullable: false),
                    IsDeleted = table.Column<bool>(type: "boolean", nullable: false),
                    last_activity_time = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    last_failed_login = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    last_login_time = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    locked_until = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    password = table.Column<string>(type: "character varying(255)", maxLength: 255, nullable: false),
                    phone = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: true),
                    salt = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
                    updated_at = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    username = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_app_users", x => x.id);
                });

            migrationBuilder.CreateIndex(
                name: "IX_app_users_email",
                table: "app_users",
                column: "email",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_app_users_phone",
                table: "app_users",
                column: "phone");

            migrationBuilder.CreateIndex(
                name: "IX_app_users_username",
                table: "app_users",
                column: "username",
                unique: true);

            migrationBuilder.AddForeignKey(
                name: "FK_conversations_app_users_user_id",
                table: "conversations",
                column: "user_id",
                principalTable: "app_users",
                principalColumn: "id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_document_chunks_documents_document_chunk_document_id",
                table: "document_chunks",
                column: "document_chunk_document_id",
                principalTable: "documents",
                principalColumn: "document_id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_documents_app_users_user_id",
                table: "documents",
                column: "user_id",
                principalTable: "app_users",
                principalColumn: "id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_knowledge_bases_app_users_user_id",
                table: "knowledge_bases",
                column: "user_id",
                principalTable: "app_users",
                principalColumn: "id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_sessions_app_users_user_id",
                table: "sessions",
                column: "user_id",
                principalTable: "app_users",
                principalColumn: "id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_user_roles_app_users_user_id",
                table: "user_roles",
                column: "user_id",
                principalTable: "app_users",
                principalColumn: "id",
                onDelete: ReferentialAction.Cascade);
        }
    }
}
